import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'

export const refreshStudentSections = assignmentInfo => {
  return {
    type: 'STUDENT_ASSIGNMENTS',
    assignmentInfo
  }
}

export const refreshExcellentQuizzes = excellentQuizzes => {
  return {
    type: 'REFRESH_EXCELLENT_QUIZZES',
    excellentQuizzes
  }
}

export const getStudentTasksSection = (programId, studentsId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/programs/${programId}/students/${studentsId}/assignments`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshStudentSections(res.body))
      }
    })()
  }
}

export const getExcellentQuizzes = (assignmentId, quizId, page = 1, pageSize = 10) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/assignments/${assignmentId}/quizzes/${quizId}/excellentQuizzes?page=${--page}&pageSize=${pageSize}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshExcellentQuizzes(res.body))
      }
    })()
  }
}

export const startAssignment = (taskId, assignmentId, userId, callback) => {
  return () => {
    (async () => {
      const res = await request.post(`../api/v2/tasks/assignments/users`,{taskId,assignmentId,userId})
      if (res.status === HTTP_CODE.CREATED) {
        callback()
      }
    })()
  }
}
